package com.cat.controller;

import cn.hutool.core.bean.BeanUtil;
import com.cat.dto.CatDTO;
import com.cat.dto.PageDTO;
import com.cat.entity.Cat;
import com.cat.entity.CatBreed;
import com.cat.query.CatQuery;
import com.cat.result.Result;
import com.cat.service.ICatBreedService;
import com.cat.service.ICatService;
import com.cat.vo.CatVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lmy
 * @since 2024-09-26
 */
@RestController
@RequestMapping("/cat")
@Tag(name = "猫咪管理")
@Slf4j
@RequiredArgsConstructor
public class CatController {

    private final ICatService catService;
    private final ICatBreedService catBreedService;

    /**
     * 查看猫咪列表
     * @param catQuery
     * @return
     */
    @PostMapping("/list")
    @Operation(summary = "查看猫咪列表")
    public Result<PageDTO> catList(@RequestBody CatQuery catQuery){
        log.info("查询部门：{}", catQuery);
        PageDTO<CatVO> catList = catService.getByCatList(catQuery);
        return Result.success(catList);
    }

    /**
     * 猫咪品种字典
     * @return
     */
    @GetMapping("/breed")
    @Operation(summary = "猫咪品种字典")
    public Result<List<CatBreed>> breed(){
        List<CatBreed> list = catBreedService.list();
        return Result.success(list);
    }

    /**
     * 修改猫咪状态
     * @param catDTO
     * @return
     */
    @PostMapping("/change")
    @Operation(summary = "修改猫咪状态")
    public Result<String> changeStatus(@RequestBody CatDTO catDTO) {
        log.info("修改信息：{}",catDTO);
        Cat cat = new Cat();
        cat.setId(catDTO.getId());
        cat.setStatus(catDTO.getStatus());
        catService.updateById(cat);

        return Result.success("成功");
    }

    /**
     * 批量删除猫咪
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    @Operation(summary = "批量删除猫咪")
    public Result<String> delete(@RequestBody Map<String,List<Integer>> ids){
        log.info("批量删除的猫咪id:{}",ids);
        List<Integer> id = ids.get("id");
        catService.removeBatchByIds(id);

        return Result.success("成功");
    }

    /**
     * 修改猫咪
     * @param catDTO
     * @return
     */
    @PostMapping("/edit")
    @Operation(summary = "修改猫咪")
    public Result<String> editUser(@RequestBody CatDTO catDTO){
        Cat cat = BeanUtil.copyProperties(catDTO, Cat.class);
        catService.updateById(cat);

        return Result.success("成功");
    }
}
